<?php

namespace app\api\controller\v2;

use app\common\controller\Frontend;
use think\facade\Db;

class User extends Frontend
{

    /**
     * 获取下级
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getJunior (): void
    {
        $type = $this->request->param('type', 0);
        #type 只能0或1
        if (!in_array($type, [0, 1])) {
            $this->error('参数错误');
        }
        $ret = Db::name('user_level')
            ->alias('l')
            ->join('ba_user u', 'l.user_id = u.id')
            ->field('u.id,u.nickname,u.avatar,l.create_time')
            ->where('parent_user_id', $this->auth->id)->where('type', $type)->select();
        $this->success('获取成功', $ret);
    }


    /**
     * 获取推广员状态
     * @return void
     */
    public function isExtend (): void
    {
        $ret = Db::name('user')->where('id', $this->auth->id)->find();
        $this->success('获取成功', ['is_extend' => $ret['is_extend'], 'extend_status' => $ret['extend_status']]);
    }


    /**
     * 推广员收益
     * @return void
     */
    public function extendMoneyInfo (): void
    {
        $ret = Db::name('user')->where('id', $this->auth->id)->find();

        #累计收益
        $total = Db::name('giveaway_log')->where('type', 5)->where('login_id', $this->auth->id)->
        sum('money');
        #今日收益
        $todayTotal = Db::name('giveaway_log')->where('type', 5)->where('login_id', $this->auth->id)
            ->whereTime('create_time', 'today')->sum('money');

        #用户累计收益
        $userTotal = Db::name('giveaway_log')->where('type', 4)->where('login_id', $this->auth->id)->
        sum('money');

        $this->success('获取成功', ['extend_money' => $ret['extend_money'], 'extend_money_total' => sprintf('%.2f', $total), 'extend_today_total' => sprintf('%.2f', $todayTotal), 'user_total' => sprintf('%.2f', $userTotal)]);
    }


    /**
     * 获取佣金记录
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCommissionRecord (): void
    {
        $userInfo = Db::name('user')->where('id', $this->auth->id)->find();
        $where = [];
        $where[] = ['l.login_id', '=', $this->auth->id];
        if ($userInfo['is_extend'] == 1 && $userInfo['extend_status'] == 1) {
            $where[] = ['l.type', '=', 5];
        } else {
            $where[] = ['l.type', '=', 4];
        }
        $list = Db::name('giveaway_log')
            ->alias('l')
            ->where($where)
            ->join('ba_order o', 'l.order_sn = o.order_sn')
            ->join('ba_user u', 'o.user_id = u.id')
            ->field('l.money,l.create_time,u.nickname,u.avatar,l.order_sn')
            ->order('l.create_time', 'desc')
            ->paginate(10);
        $this->success('获取成功', [
            'list'  => $list->items(),
            'total' => $list->total(),
        ]);
    }

}